<?php
	# Defaults
	$FileOut = 0;
	$TrnID = $_GET['TrsID'];
	
	# Required 
	
	$GetTrn = $GLOBALS['MYT']->db_array("SELECT Trs_ID, Trs_Corr,Trs_Time,Trs_CorrTrans,CorrAgente, EXT, DELIMITER, OB, VERSION, ffFilenameFormat FROM transmitions 
											INNER JOIN corresponsales ON CorrNumero = Trs_Corr
											LEFT OUTER JOIN hd_fileformats ON CorrFormatoFichero = REF
											WHERE Trs_ID={$TrnID}");
	
	if(!isset($GetTrn[1]['EXT'])) { die("Impossivel continuar, Correspondente nao existe interface para este correspondente."); }
		# Consulta a passar para tabela de contadores para minimizar consultas a facturas
		$getCNC = $GLOBALS['MYT']->db_array("SELECT MIN(FraReferenciaCo) AS CNC FROM facturas WHERE FraOrdenTrans = {$TrnID}");
	$TrnCorrID = $GetTrn[1]['Trs_CorrTrans'];
	$CNC =$getCNC[1]['CNC']-1;
	$CorrID = $GetTrn[1]['Trs_Corr'];
	$CorrCode  = $GetTrn[1]['CorrAgente'];
	$CorrExt = $GetTrn[1]['EXT'];
	$CorrFName = $GetTrn[1]['ffFilenameFormat'];
	$TrnTime = $GetTrn[1]['Trs_Time'];
	
	//============== Gen TRANSMISSION FILE
	$QurTrn = "SELECT 
					CONCAT(FraOrigen0,'-',LPAD(FraReferenciaAg,6,0)) AS ORInternal,
					date_format(FraFecha, '%m/%d/%Y') AS FraFecha,
					date_format(FraFecha, '%Y%m%d') AS FraFecha2,
					date_format(FraFecha, '%Y-%m-%d') AS FraFecha3,
					date_format(FraFecha, '%h:%i:%s') AS FraHora,
					FraReferenciaAg,
					FraReferenciaCo,
					FraComisionCorr,
					FraTasa,
					FraMensaje,
					FraStatus,
					BnfCedula,
					BnfNombre,
					BnfTelefono,
					BnfBarrio,
					BnfBarrio AS BNFNOTES,
					BnfDireccion,
					BnfProvincia,
					BnfTelefono,
					BnfTelefono2,
					ciudades.CiuCiudad AS BnfCiudad,
					hd_paises.pai_NombreUSD AS BnfPais,
					hd_paises.pai_Sufijo AS BnfPaisCode,
					hd_paises.pai_CodeIso AS BnfPaisISONum,
					bp_IdBanco,
					cb_Banco,
					cb_Sucursal,
					cb_Cuenta,
					cb_Cuenta_prx,
					cb_Cuenta_sux,
					cb_Cuenta_mod,
					cb_TipoCuenta,
					CorrNumero,
					CorrAgente,
					hf_actypes.SHORT_NAME AS TipoCuenta,
					CliNombre,
					CliDNI,
					CliDomicilio,
					CliCP,
					CliTelefono,
					cl_country.pai_NombreESP AS CliPais,
					cl_country.pai_Sufijo AS CliPaisCode,
					cl_country.pai_CodeIso AS CliPaisISONum,
					cl_city.CiuCiudad AS CliCiudad,
					ROUND(FraEnviado) AS FraEnviado,
					ROUND(FraARecibir,0) AS FraARecibir,
					CobMon.miso_ID AS FraMonCobro,
					PagMon.miso_ID AS FraMonDPago,
					bp_Name,
					tcn_Short AS FraModo,
					OFCorrCode AS FraAgenciaPago,
					mp_short AS FraModoPago
				FROM facturas IGNORE INDEX (IdxCli)
				INNER JOIN clientes ON CliNumero = FraCliNumero
				INNER JOIN beneficiarios ON BnfNumero = FraBnfNumero
				INNER JOIN corresponsales ON CorrNumero = FraCorresponsal
				INNER JOIN hd_transactionconcept ON tcn_ID = FraModo
				LEFT OUTER JOIN ciudades ON CiuNumero = BnfCiudad
				LEFT OUTER JOIN ciudades AS cl_city		ON cl_city.CiuNumero = CliCiudad
				INNER JOIN hd_paises ON pai_CodElite = BnfPais
				INNER JOIN hd_paises AS cl_country	ON cl_country.pai_CodElite = ClPais
				INNER JOIN hd_monedasiso AS CobMon ON CobMon.miso_cod_elite = FraMonCobro
				INNER JOIN hd_monedasiso AS PagMon ON PagMon.miso_cod_elite = FraMonDPago
				INNER JOIN hd_modospago ON mp_ID = FraModoPago
				LEFT OUTER JOIN beneficiarioscuentas ON cb_Id = FraCtaBenef
				LEFT OUTER JOIN hf_actypes ON hf_actypes.REF = cb_TipoCuenta
				LEFT OUTER JOIN hd_bancos ON bp_ID = cb_Banco
				LEFT OUTER JOIN oficcorresp ON OFId = FraAgenciaPago
				WHERE FraOrdenPago IN ({$ORPOOL})
				AND FraOrdenTrans IS NULL
				AND FraStatus = 3
				AND FraCorresponsal = {$CorrespID}
				AND FraModo = 1";

 	# Buscar Template de Formato de Transaccao	
	include($TFTPATH_MODELS.$GetTrn[1]['DELIMITER'].'_'.$GetTrn[1]['EXT'].'_'.$GetTrn[1]['OB'].'_'.$GetTrn[1]['VERSION'].'_'.$PID.'.mex');
	
	
	# Transforma nome do ficheiro de Transaccao
	$TransactionFTName = transaction_filenames($DatadirTransmitionOut,$TrnTime,$TrnID,$CorrCode,$CorrFName,$CorrExt);
	
	# Rename
	rename($TransactionFTName,$TransactionFTName.'_del'.date("his"));
	
	if (File_Output($DatadirTransmitionOut.$TransactionFTName,$QurTrn,$CNC)==true) {
		$FileOut = 2;
	}
	echo 'Done.';